/**
 * @Title: ExampleFile.java
 * @Package com.example.package
 * @Description: Placeholder (briefly describe the purpose of this file)
 * @Author: Evan
 * @Date: 2023-08-18
 * @Version: V1.0
 *
 * Copyright Notice:
 * This project is released in open source and follows the applicable open source license.
 * All rights reserved. (c) 2023 Evan.
 * This project is protected by copyright laws and international copyright treaties.
 * Unauthorized reproduction, sale, copy, modification, merge, publication, distribution,
 * licensing, and/or sales of this software and its copies are prohibited without permission.
 * Please contact the author for authorization.
 * Contact Information:
 * @Email: uyevan@163.com
 */

package com.evan.biznak.vip.Activity;

import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.bumptech.glide.Glide;
import com.evan.biznak.vip.Adapter.StartPageAdapter;
import com.evan.biznak.vip.Class.AppData;
import com.evan.biznak.vip.Fragment.KixmixFragment;
import com.evan.biznak.vip.Fragment.KoznakFragment;
import com.evan.biznak.vip.R;
import com.evan.biznak.vip.Util.SharePreferencesUtils;
import com.evan.biznak.vip.Util.WindowUtils;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;

import es.dmoral.toasty.Toasty;

public class StartActivity extends AppCompatActivity {
    @SuppressLint({"MissingInflatedId", "LocalSuppress"})
    /* QQ群 */
    private MaterialButton join_group;

    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_start);
        /*赞助*/
        if (AppData.isSPONSOR()) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    sponsor();
                }
            }, 3000);
        }
        // 状态栏设置
        WindowUtils.setStatusColor(this, R.color.white);
        // TabLayout
        TabLayout main_tab = findViewById(R.id.main_tab);
        // 创建 Fragment 列表
        ArrayList<Fragment> fragmentList = new ArrayList<>();
        fragmentList.add(new KixmixFragment());
        fragmentList.add(new KoznakFragment());
        // 创建 ViewPager 和 TabLayout，并绑定适配器
        ViewPager2 viewPager = findViewById(R.id.main_viewPager);
        viewPager.setAdapter(new StartPageAdapter(this, fragmentList));
        /*ViewPager 滑动时间*/
        viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                main_tab.selectTab(main_tab.getTabAt(position));
            }
        });
        /*TabLayout 选中事件*/
        main_tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
        /*Menu*/
        MaterialToolbar toolbar = findViewById(R.id.tool_bar);
        toolbar.setOnMenuItemClickListener(item -> {
            String title = String.valueOf(item.getTitle());
            Log.e("title", title);
            switch (title) {
                case "清除数据":
                    SharePreferencesUtils.clear(StartActivity.this);
                    Toasty.info(StartActivity.this, "已清除所有数据，请重启", Toasty.LENGTH_SHORT, true).show();
                    return true;
                case "关于软件":
                    new MaterialAlertDialogBuilder(StartActivity.this).setIcon(R.drawable.twotone_cruelty_free_24)
                            .setPositiveButton(R.string.联系, (dialog, which) -> startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("mqqwpa://im/chat?chat_type=wpa&uin=" + "3487297072" + "&version=1"))))
                            .setNegativeButton(R.string.取消, null)
                            .setTitle(R.string.关于Biznak的说明)
                            .setMessage(R.string.biznak_24)
                            .setCancelable(false)
                            .show();
                    return true;
                case "服务协议":
                    final AlertDialog dialog = new MaterialAlertDialogBuilder(StartActivity.this)
                            .setPositiveButton(R.string.我同意, null)
                            .setNegativeButton(R.string.我拒绝, null)
                            .setCancelable(false)
                            .setTitle(R.string.biznak服务协议)
                            .setMessage(getString(R.string.agree))
                            .create();
                    dialog.setOnShowListener(dialogs -> {
                        Button N_button = dialog.getButton(DialogInterface.BUTTON_NEGATIVE);
                        Button P_button = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
                        /*接受*/
                        P_button.setOnClickListener(v -> {
                            Toasty.success(StartActivity.this, "你已接受协议", Toasty.LENGTH_LONG, true).show();
                            SharePreferencesUtils.saveString(StartActivity.this, "agreement", "true");
                            dialog.dismiss();
                        });
                        /*拒绝*/
                        N_button.setOnClickListener(v -> {
                            Toasty.error(StartActivity.this, "你拒绝协议", Toasty.LENGTH_LONG, true).show();
                            SharePreferencesUtils.clear(StartActivity.this, "agreement");
                            dialog.dismiss();
                            finish();
                        });
                    });
                    dialog.show();
                    return true;
                case "官方群聊":
                    if (!joinQQGroup(AppData.getQqKey())) {
                        Toasty.info(StartActivity.this, "请安装QQ并重启应用", Toasty.LENGTH_SHORT, true).show();
                    }
                    return true;
                case "赞助作者":
                    sponsor();
                    return true;
                default:
                    return false;
            }
        });
    }

    /*赞助*/
    public void sponsor() {
        @SuppressLint("InflateParams") View view = getLayoutInflater().inflate(R.layout.dia_sponsor_view, null);
        final BottomSheetDialog dialog = new BottomSheetDialog(StartActivity.this);
        dialog.setContentView(view);
        dialog.setCancelable(true);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setDismissWithAnimation(true);
        /* ID获取 */
        join_group = view.findViewById(R.id.join_group);
        ImageView wx = view.findViewById(R.id.vx);
        ImageView zfb = view.findViewById(R.id.zfb);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Glide.with(StartActivity.this).load(AppData.getWxQr()).error(R.mipmap.app_icon).into(wx);
                Glide.with(StartActivity.this).load(AppData.getZfbQr()).error(R.mipmap.app_icon).into(zfb);
            }
        });
        join_group.setOnClickListener(v -> {
            if (!joinQQGroup(AppData.getQqKey())) {
                Toasty.info(StartActivity.this, "请安装QQ并重启应用", Toasty.LENGTH_SHORT, true).show();
            }
        });
        dialog.show();
    }

    public boolean joinQQGroup(String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse("mqqopensdkapi://bizAgent/qm/qr?url=http%3A%2F%2Fqm.qq.com%2Fcgi-bin%2Fqm%2Fqr%3Ffrom%3Dapp%26p%3Dandroid%26jump_from%3Dwebapi%26k%3D" + key));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            return false;
        }
    }
}